package com.loan.util;

import java.security.MessageDigest;

public class MD5Util {
   /**
    * 将指定的字符串用MD5加密
    * data需要加密的字符串
    * @param data
    * @return
    */
   public static String encrypt(String data) {
	   try {
	       MessageDigest md = MessageDigest.getInstance("MD5");
	       md.update(data.toString().getBytes());
	       return bytesToHex(md.digest());
	   }catch(Exception e) {
		   e.printStackTrace();
	   }
	return data;
   }
   
   private static String bytesToHex(byte[] ch) {
       StringBuffer ret = new StringBuffer("");
       for (int i = 0; i < ch.length; i++)
           ret.append(byteToHex(ch[i]));
       return ret.toString();
   }
   /**
    * 字节转换为16进制字符串
    */
   private static String byteToHex(byte ch) {
       String str[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
       return str[ch >> 4 & 0xF] + str[ch & 0xF];
   }
}
